<?php
namespace Mobile\Controller;
use Think\Controller;
class CrmController extends CommunalController{
	//项目--列表
	function project_list(){
		$db_project_seach=M('db_project_seach'); 
		//列表
		$db_project=M('db_crm_project');
		$string=NULL;
		if(!empty($_REQUEST['keyword']))
		{
			$map['project_num']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['name']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['_logic']='or';
			$where['_complex']=$map;
			$this->assign('keyword',$_REQUEST['keyword']);
			$add_where['keyword']=$_REQUEST['keyword'];
			$add_where['add_uid']=$this->login_id;
            $select_res=$db_project_seach->where($add_where)->find();
            if(empty($select_res)){
                $add_where['add_date']=date('Y-m-d H:i:s');
                $db_project_seach->add($add_where);
            }
		}

		if($this->get_is_project_function()){
			$db_project_function=M('db_project_function'); 
			$where_f['uid']=$this->login_id;
			$where_f['start_date']=array('elt',date("Y-m-d"));
			$where_f['end_date']=array('egt',date("Y-m-d"));
			$result_f=$db_project_function->where($where_f)->select();
			foreach($result_f as $key=>$value){
				$pids.=$value['pid'].',';
			}
			$string.="(id in (".rtrim($pids,',').")) or";
		}
		if($string){$where['_string']=rtrim($string,'or');}
		//记录取6条
		$result=$db_project->where($where)->order('project_id desc')->limit(6)->select();

		//记录总条数
		$count=$db_project->where($where)->count();
		foreach($result as $key=>$value)
		{	
			if($_REQUEST['keyword']){
				$result[$key]['project_num']=preg_replace("/".$_REQUEST['keyword']."/i", "<font color=#fb607f>".$_REQUEST['keyword']."</font>",$value['project_num']);
				$result[$key]['name']=preg_replace("/".$_REQUEST['keyword']."/", "<font color=#fb607f>".$_REQUEST['keyword']."</font>",$value['name']);
			}
			if($value['stage']==""){
				$result[$key]['stage']="";
			}
		}
		$this->assign('result',$result);
		$this->assign('count',$count);
		//推荐搜索
		// $where_s['center_id']=$center_id;
		$result_t=$db_project_seach->field('keyword,count(id) as ct')->where($where_s)->group('keyword')->order('ct desc,add_date desc')->limit(6)->select();
		$this->assign('result_t',$result_t);
		//搜索历史
		// $where_l['center_id']=$center_id;
		$where_l['add_uid']=$this->login_id;
		$result_l=$db_project_seach->field('keyword,max(add_date) as md')->where($where_l)->group('keyword')->order('md desc')->limit(6)->select();
		$this->assign('result_l',$result_l);
		$this->display();
	}
	public function project_list_ajax(){
		$start=$_REQUEST['start_size'];
		$end=$_REQUEST['start_page'];
		$db_project_seach=M('db_project_seach'); 
		//列表
		$db_project=M('db_crm_project');
		$string=NULL;
		if(!empty($_REQUEST['keyword']))
		{
			$map['project_num']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['name']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['_logic']='or';
			$where['_complex']=$map;
			$this->assign('keyword',$_REQUEST['keyword']);
			$add_where['keyword']=$_REQUEST['keyword'];
			$add_where['add_uid']=$this->login_id;
            $select_res=$db_project_seach->where($add_where)->find();
            if(empty($select_res)){
                $add_where['add_date']=date('Y-m-d H:i:s');
                $db_project_seach->add($add_where);
            }
		}

		if($this->get_is_project_function()){
			$db_project_function=M('db_project_function'); 
			$where_f['uid']=$this->login_id;
			$where_f['start_date']=array('elt',date("Y-m-d"));
			$where_f['end_date']=array('egt',date("Y-m-d"));
			$result_f=$db_project_function->where($where_f)->select();
			foreach($result_f as $key=>$value){
				$pids.=$value['pid'].',';
			}
			$string.="(id in (".rtrim($pids,',').")) or";
		}
		if($string){$where['_string']=rtrim($string,'or');}
		//记录取6条
		$result=$db_project->where($where)->order('project_id desc')->limit($start,$end)->select();
		//记录总条数
		$count=$db_project->where($where)->count();
		foreach($result as $key=>$value)
		{	
			if($_REQUEST['keyword']){
				$result[$key]['project_num']=preg_replace("/".$_REQUEST['keyword']."/i", "<font color=#fb607f>".$_REQUEST['keyword']."</font>",$value['project_num']);
				$result[$key]['name']=preg_replace("/".$_REQUEST['keyword']."/", "<font color=#fb607f>".$_REQUEST['keyword']."</font>",$value['name']);
			}
			
			if($value['stage']!=""){
				$result[$key]['stage']=get_project_stage($value['stage']);
			}else{
				$result[$key]['stage']="";
			}
			if($value['leader']){
				$result[$key]['leader']=get_userName($value['leader']);
			}
			
		}
		$this->assign('result',$result);
		$this->assign('count',$count);
		//推荐搜索
		echo json_encode($result);die;
	}
	//项目-->详细页
	function project_situation(){
		//项目详情
		$db_contacts=M('db_crm_contact');
		$project_id = $_REQUEST['id'];
		$project_list=$this->get_project_new($project_id);
		//跟进记录
		$res_follow = M("db_crm_follow_record")->where("project_id=$project_id")->order('add_date desc')->select();
		if(!empty($res_follow)){
			foreach($res_follow as $k=>$rf){
				$where_c['contact_id']=$rf['contact_id'];
				$cids=$db_contacts->where($where_c)->find();
				$res_follow[$k]['ouser']=$cids['username'];
				$res_follow[$k]['work_type']=$this->get_work_type($rf['work_type']);
			}
		}
		$work_chance = array(1=>"顺利",2=>"一般",3=>"困难",4=>"不确定");
		$this->assign('project',$project_list);
		$this->assign('result',$res_follow);
		$this->assign('work_chance',$work_chance);
		$this->display();
	}
	//项目-->基本信息页
	public function project_detail(){
		$project_id = I("id");
        $res_project = M("db_crm_project")->where("project_id=$project_id")->find();
        $information_id = $res_project['information_id'];
        $information = M("db_crm_customers_information")->where("information_id=$information_id")->find();
        $this->assign('project',$res_project);
        $this->assign('information', $information);
        $this->display();
	}
	//项目--联系人
	public function project_contact(){
		$project_id = I("id");
		$res_project = M("db_crm_project")->field("other_linkman,project_num,name,project_id")->where("project_id=$project_id")->find();
		if($res_project['other_linkman']!=""){
			$res_contact = M("db_crm_contact")->where("contact_id in (".$res_project['other_linkman'].")")->select();
		}else{
			$res_contact = array();
		}
		$gender=array('1'=>'男','2'=>'女');
		$job_level=$this->get_constant(C("JOB_LEVEL"));
		$relation=$this->get_constant(C("DECISION_RELATION"));
		$this->assign('project',$res_project);
		$this->assign('gender',$gender);
		$this->assign('job_level',$job_level);
		$this->assign('relation',$relation);
		$this->assign('result',$res_contact);
		$this->display();
	}
	//项目--联系人添加
	public function project_contact_add(){
		$project_id = I("project_id");
		$db_province_city=M('db_province_city');
		$where_p['reid']=0;
		$result_p=$db_province_city->where($where_p)->select();
		$caty_arr=array();
		foreach($result_p as $key=>$value){
			$caty_arr[$key]['value']=$value['id'];
			$caty_arr[$key]['text']=$value['name'];
			$where_c['reid']=$value['id'];
			$result_c=$db_province_city->where($where_c)->select();
			foreach($result_c as $k=>$v){
				$caty_arr[$key]['children'][$k]['value']=$v['id'];
				$caty_arr[$key]['children'][$k]['text']=$v['name'];
			}
		}
		$this->assign('project_id',$project_id);
		$this->assign('caty_arr',json_encode($caty_arr));
		$this->display();
	}
	public function project_contact_doadd(){
		$db_contact = M("db_crm_contact");
		if($_POST['action']=='add'){
			$add_data = $this->unset_null($_POST,$add_data);//清洗提交的空数据
			$add_data['add_uid'] = $_SESSION['dms_uid'];
			$add_data['add_user'] = $_SESSION['dms_username'];
			$add_data['add_date'] = date('Y-m-d H:i:s');
			$last_id = $db_contact->add($add_data);
			$this->update_project_contact($_POST['pid'],$last_id);//更新联系人
		}elseif($_POST['action']=='edit'){
			$edit_data['contact_id']=$_POST['contact_id'];
			$edit_data = $this->unset_null($_POST,$edit_data);//清洗提交的空数据
			$last_id = $db_contact->save($edit_data);
		}
		if($last_id)
		{
			$data['status']=self::YES;
			$data['msg']='操作成功';
		}
		else
		{
			$data['status']=self::NO;
			$data['msg']='操作失败';
		}
		echo json_encode($data);die;
	}
	//项目--资料
	public function	project_file(){
		$project_id = I("id");
		$res_project = M("db_crm_project")->field("other_linkman,project_num,name,project_id")->where("project_id=$project_id")->find();
		$where['project_id']=$project_id;
		$res_uploads = M("db_crm_project_upload")->field("upload_id,file_name,file_url")->where($where)->select();
		$this->assign('project',$res_project);
		$this->assign('result',$res_uploads);
		$this->display();
	}
	//项目--资料添加
	public function	project_file_add(){
		$project_id = I("project_id");
		$this->assign('project_id',$project_id);
		$this->display();
	}
	public function project_file_doadd(){
		print_r($_POST['file']);
		die("==23==");
	}
	//项目--跟进记录添加
	public function project_follow_add(){
		$project_id = I("id");
		$res_project = M("db_crm_project")->field("other_linkman,project_num,name,project_id,stage")->where("project_id=$project_id")->find();
		//跟进方式
		$follow_type=$this->get_constant(C("FOLLOW_TYPE"));
		//项目阶段
		$status=$this->get_constant(C("PROJECT_PHASE"));
		$contact = $this->getContactByProjectLinkman($res_project['other_linkman']);
		$this->assign('follow_type',$follow_type);
		$this->assign('status',$status);
		$this->assign('contact',$contact);
		$this->assign('stage',$res_project['stage']);
		$this->assign('project',$res_project);
		$this->display();
	}
	//项目--跟进记录编辑
	public function project_follow_edit(){
		$db_follow=M('db_crm_follow_record');
		$where_t['record_id']=I("id");
		$result=$db_follow->where($where_t)->find();
		$where = "project_id=".$result['project_id'];
		$res_project = M("db_crm_project")->field("other_linkman,project_num,name,project_id,stage")->where($where)->find();
		//跟进方式
		$follow_type=$this->get_constant(C("FOLLOW_TYPE"));
		//项目阶段
		$status=$this->get_constant(C("PROJECT_PHASE"));
		$contact = $this->getContactByProjectLinkman($res_project['other_linkman']);
		$this->assign('result',$result);
		$this->assign('follow_type',$follow_type);
		$this->assign('status',$status);
		$this->assign('contact',$contact);
		$this->assign('project',$res_project);
		$this->display();
	}
	public function stage_ajax(){
		$db_follow = M('db_crm_follow_record');
		$db_project = M('db_crm_project');
		$edit_project=array();
		if($_REQUEST['action']=='follow_add'){
			$where['project_id']=$_REQUEST['pid'];
			$where['visit_date']=$_REQUEST['visit_date'];
			$where['work_chance']=$_REQUEST['work_chance'];
			$where['contact_id']=$_REQUEST['type3'];
			$where['follow_type']=$_REQUEST['type1'];
			$where['remark']=$_REQUEST['remark'];
			$where['status']=$_REQUEST['type2'];
			if(!empty($_REQUEST['work_type'])){
				$where['work_type']=join(',',$_REQUEST['work_type']);
			}
			$where['add_uid']=$this->login_id;
			$where['add_user'] = $_SESSION['dms_username'];
			$where['add_date']=date('Y-m-d H:i:s');
			$now = date("Y-m-d");
			$timeInfo = $this->getNowTimeInfo($now);
			$where['year']=$timeInfo['year'];
			$where['week']=$timeInfo['week'];
			$where['year_week']=$timeInfo['year'].$timeInfo['week'];
			$result_bool=$db_follow->add($where);
			//更新流程表
			$this->update_project_flow($_REQUEST['pid'],$_REQUEST['type2'],$_REQUEST['remark'],$this->login_id,$_SESSION['dms_username'],date('Y-m-d H:i:s'));
			//更新项目表状态
			$edit_project['project_id']=$where['project_id'];
			$edit_project['stage']=$where['status'];
			$db_project->save($edit_project);
		}elseif($_REQUEST['action']=='follow_edit'){
			$edit_data = array();
			$edit_data['record_id']=$_REQUEST['record_id'];
			if(!empty($_REQUEST['work_type'])){
				$edit_data['work_type']=join(',',$_REQUEST['work_type']);
				unset($_REQUEST['work_type']);
			}
			if(!empty($_REQUEST['type1'])){
				$_REQUEST['follow_type']=$_REQUEST['type1'];
				unset($_REQUEST['type1']);
			}
			if(!empty($_REQUEST['type2'])){
				$_REQUEST['status']=$_REQUEST['type2'];
				unset($_REQUEST['type2']);
			}
			if(!empty($_REQUEST['type3'])){
				$_REQUEST['contact_id']=$_REQUEST['type3'];
				unset($_REQUEST['type3']);
			}
			$edit_data = $this->unset_null($_REQUEST,$edit_data);//清洗提交的空数据
			$result_bool=$db_follow->save($edit_data);
			//更新流程表
			$this->update_project_flow($edit_data['project_id'],$edit_data['status'],$edit_data['remark'],$this->login_id,$_SESSION['dms_username'],date('Y-m-d H:i:s'));
			//更新项目表状态
			$edit_project['project_id']=$edit_data['project_id'];
			$edit_project['stage']=$edit_data['status'];
			$db_project->save($edit_project);
		}elseif($_REQUEST['action']=='tp_status'){
			$db_project=M('db_project');
			$project=parent::get_project($_REQUEST['id']);
			if($project['qy_status']==1 or $project['gt_status']==-1 or $project['gt_status']==NULL){
				if($project['tp_status']==$_REQUEST['status']){}
			}else{
				$where['id']=$_REQUEST['id'];
				$where['tp_status']=$_REQUEST['status'];
				$result_bool=$db_project->save($where);
			}
		}
		if($result_bool)
		{
			$data['status']=self::YES;
			$data['msg']='操作成功';
		}
		else
		{
			$data['status']=self::NO;
			$data['msg']='操作失败';
		}
		echo json_encode($data);die;
	}

	//项目添加页
	public function project_add()
    {
        $information_id = I('information_id');
        $userList = get_userName('');
        $db_crm_customers_information = M("db_crm_customers_information");//市场信息表
        $db_crm_project = M("db_crm_project");
        $db_crm_project_flow = M("db_crm_project_flow");//项目状态记录表
        $res_information = $db_crm_customers_information->field('information_id,customer_name')->select();//市场信息表信息
        $map['project_num'] = array('like', array('LF%'));
        $project_num = $db_crm_project->field('project_num')->where($map)->order('project_num desc')->limit(1)->find();
        $project_num = !empty($project_num['project_num']) ? substr($project_num['project_num'],2) : '0000';
        $num = intval($project_num);
        //echo sprintf("%04d", $num+1);
        $project_num = 'LF' . str_pad($num + 1, 4, "0", STR_PAD_LEFT);//自增编号
        if (!empty($_POST)) {
            $X = $_POST['nature'];
            $Y = $_POST['type'];
            $data = $_POST;
            $data['project_num'] = $project_num . '-' . $X . 'X-' . $Y . 'Y';
            $data['add_uid'] = $flow_data['change_uid'] = $flow_data['add_uid'] = $_SESSION['dms_uid'];
            $data['add_date'] = $flow_data['add_date'] = $flow_data['change_date'] = date('Y-m-d H:i:s');
            try {
                $project_id = $db_crm_project->add($data);
                $flow_data['project_id'] = $project_id;
                $flow_data['stage'] = $_POST['stage'];
                $flow_data['add_user'] = $_SESSION['dms_username'];
                $flow_data['remark'] = '录入项目时初始状态';
                $db_crm_project_flow->add($flow_data);
                $this->success("操作成功", __APP__ . "/Mobile/Crm/project_list");
                die;
            } catch (\Exception $e) {
                $this->error("操作失败", __APP__ . "/Mobile/Crm/project_list");
                die;
            }
        }
        //添加主要联系人
        $db_crm_contact = M("db_crm_contact");
        $res_crm_contact = $db_crm_contact->select();
        $this->assign('user_list', $userList);
        $this->assign('province_list', $this->get_province_city());
        $this->assign('job_level', $this->get_constant(C("JOB_LEVEL")));
        $this->assign('relation', $this->get_constant(C("DECISION_RELATION")));
        $this->assign('res_crm_contact', $res_crm_contact);
        $this->assign('res_information', $res_information);
        $this->assign('now', date('Y-m-d'));
        $this->assign('information_id', $information_id);
        $this->display();
    }
	public function test(){
		$this->assign('mobile','18017383197');
		$this->display();
	}
}
?>